/** 
 * DefaultInningLabelParser.java Created by iweerarathna on Sep 1, 2011.
 * 
 * Copyright (c) 2011 Virtusa Corporation. All Rights Reserved.<br><br>. 
 * 
 */
package org.uim.cricket.analyzer.parsers.inning.impl;

import org.uim.cricket.analyzer.parsers.inning.InningHeaderLabelParsable;
import org.uim.cricket.entities.inning.InningLabel;
import org.uim.cricket.util.HTMLStringUtils;
import org.uim.cricket.util.MyStringUtils;

/**
 * TODO Class description goes here.
 * @version 
 * @author iweerarathna
 */
public class DefaultInningHeaderLabelParser implements InningHeaderLabelParsable {

    @Override
    public InningLabel parseInnLabel(String source) {
        if (source == null || source.length() == 0) return null;
        InningLabel innl = new InningLabel();

        if (source.endsWith(" team")) {
        	innl.setBattedTeam(source.substring(0, source.length() - 5).trim());
        	return innl;
        }
        
        String title = HTMLStringUtils.cleanHTMLTags(source);
        int pos = title.indexOf('(');
        if (pos < 0) {
            
            innl.setBattedTeam(this.getTeamName(title));
            
        } else {
            
            innl.setBattedTeam(this.getTeamName(title.substring(0, pos).trim()));
            title = title.substring(pos + 1).trim();
            String[] parts = title.split("[ ]+");
            
            if (MyStringUtils.isNumeric(parts[0])) {
                innl.setMaxOvers(parts[0]);
            } else {
                
                if (parts[0].equalsIgnoreCase("target:")) {
                    
                    for (int i=1;i<parts.length;i++) {
                        if (MyStringUtils.isNumeric(parts[i])) {
                            if (innl.getTarget() <= 0) {
                                innl.setTarget(Integer.parseInt(parts[i]));
                            } else {
                                innl.setTargetChaseOvers(parts[i]);
                            }
                        }
                    }
                    
                }
                
            }
            
            
        }
        
        return innl;
    }

    private String getTeamName(String source) {
        if (source == null || source.length() == 0) return null;
        String[] parts = source.split("[ ]+");
        
        if (parts.length == 0) return null;
        int endindex = parts.length - 1;
        
        if (parts[parts.length-1].startsWith("inning")) {
            endindex--;
            for (int i=parts.length-2;i>=0; i--) {
                if (parts[i].charAt(0) >= '0' && parts[i].charAt(0) <= '9') {
                    endindex--;
                    continue;
                }
                break;
            }
        }
        
        StringBuilder team = new StringBuilder();
        for (int i=0;i<=endindex;i++) {
            if (i > 0) team.append(' ');
            team.append(parts[i]);
        }
        
        return team.toString();
    }
    
}
